package mk.jug.academy.service;


import java.util.Date;

import mk.jug.academy.dao.StudentReviewDAO;
import mk.jug.academy.model.Lecturer;
import mk.jug.academy.model.Page;
import mk.jug.academy.model.Student;
import mk.jug.academy.model.StudentReview;
import mk.jug.academy.model.StudentReviewStatus;
import mk.jug.academy.preconditions.StudentReviewPreconditions;
import mk.jug.academy.util.Utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * Provides the business logic implementation for the {@link StudentReviewService}.
 *
 * @author Ice.Penov
 */
@Service
public class StudentReviewServiceImpl implements StudentReviewService {
	
	@Autowired private StudentReviewDAO studentReviewDAO;
	
	@Override
	public void startReview(Student student, Lecturer lecturer, StudentReview studentReview) {
		StudentReviewPreconditions.startReview(student, lecturer, studentReview);
		studentReview.setStudentID(student.getId());
		studentReview.setStudentDisplayName(student.getDisplayName());
		studentReview.setLecturerID(lecturer.getId());
		studentReview.setLecturerDisplayName(lecturer.getDisplayName());
		studentReview.setStatus(StudentReviewStatus.BEING_DEFINED);
		studentReview.setReview("Review started by " + lecturer.getDisplayName() + " on " + new Date());
		studentReviewDAO.save(studentReview);
	}

	@Override
	public Page<StudentReview> listCurrentlyDefined(int pageNumber, int itemsPerPage) {
		return studentReviewDAO.findByStatus(StudentReviewStatus.BEING_DEFINED, Utils.calculateStart(pageNumber, itemsPerPage), itemsPerPage);
	}
	
	@Override
	public Page<StudentReview> listCompleted(int pageNumber, int itemsPerPage) {
		return studentReviewDAO.findByStatus(StudentReviewStatus.COMPLETE, Utils.calculateStart(pageNumber, itemsPerPage), itemsPerPage);
	}
	
	@Override
	public Page<StudentReview> listDeleted(int pageNumber, int itemsPerPage) {
		return studentReviewDAO.findByStatus(StudentReviewStatus.DELETED, Utils.calculateStart(pageNumber, itemsPerPage), itemsPerPage);
	}
	
	@Override
	public void continueReview(StudentReview studentReview) {
		StudentReviewPreconditions.continueReview(studentReview);
		studentReviewDAO.save(studentReview);
	}
	
	@Override
	public StudentReview load(Long id) {
		return studentReviewDAO.find(StudentReview.class, id);
	}
	
	@Override
	public void completeReview(StudentReview studentReview) {
		StudentReviewPreconditions.completeReview(studentReview);
		studentReview.setStatus(StudentReviewStatus.COMPLETE);
		studentReviewDAO.save(studentReview);
	}	
	
	@Override
	public void deleteReview(StudentReview studentReview) {
		StudentReviewPreconditions.deleteReview(studentReview);
		studentReview.setStatus(StudentReviewStatus.DELETED);
		studentReviewDAO.save(studentReview);
	}

}